package org.javver.xmpp.old;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Pawel 'Pablo' Tomaszewski
 * TODO: do przemyslenia cala koncepcja properties'ow, szczegolnie zapis i uaktualnienia
 * 
 * Aby dodac nowe properties:
 * - dopisz je do pliku javver.properties
 * - stworz odpowiadajace temu wpisowi, statyczne pole w niniejszej klasie
 * - w bloku static{} przypisz mu wartosc z pliku .properties
 * - stworz mozliwosc zapisywania tego properties w metodzie commit()
 */

public class OldJavverProperties extends Properties {
	public static String HOST, USERNAME, PASSWORD, JID, WINDOW_TITLE;
	public static boolean IS_DEBUG;
	public static int WINDOW_HEIGHT, WINDOW_WIDTH, WINDOW_POSITION_X, WINDOW_POSITION_Y, PORT;

    private static final Logger logger = Logger.getLogger(OldJavverProperties.class);

	static {
		OldJavverProperties props = new OldJavverProperties();
		
		try {

            File propertiesFile = new File("src/main/resources/javver.properties");
			props.load(new FileInputStream(propertiesFile));            

        } catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		JID = props.getProperty("JID");
		USERNAME = props.getProperty("USERNAME");
		PASSWORD = props.getProperty("PASSWORD");
		HOST = props.getProperty("HOST");
		PORT = Integer.parseInt(props.getProperty("PORT"));
		IS_DEBUG = Boolean.parseBoolean(props.getProperty("IS_DEBUG"));
		
		//gui settings
		WINDOW_TITLE = props.getProperty("WINDOW_TITLE");
		WINDOW_HEIGHT = Integer.parseInt(props.getProperty("WINDOW_HEIGHT"));
		WINDOW_WIDTH = Integer.parseInt(props.getProperty("WINDOW_WIDTH"));
		WINDOW_POSITION_X = Integer.parseInt(props.getProperty("WINDOW_POSITION_X"));
		WINDOW_POSITION_Y = Integer.parseInt(props.getProperty("WINDOW_POSITION_Y"));
	}
	
	public static void commit() {
		OldJavverProperties props = new OldJavverProperties();
		
		props.setProperty("JID", JID);
		props.setProperty("USERNAME", USERNAME);
		props.setProperty("PASSWORD", PASSWORD);
		props.setProperty("HOST", HOST);
		props.setProperty("PORT", Integer.toString(PORT));
		props.setProperty("IS_DEBUG", Boolean.toString(IS_DEBUG));
		props.setProperty("WINDOW_TITLE", WINDOW_TITLE);
		props.setProperty("WINDOW_HEIGHT", Integer.toString(WINDOW_HEIGHT));
		props.setProperty("WINDOW_WIDTH", Integer.toString(WINDOW_WIDTH));
		props.setProperty("WINDOW_POSITION_X", Integer.toString(WINDOW_POSITION_X));
		props.setProperty("WINDOW_POSITION_Y", Integer.toString(WINDOW_POSITION_Y));
		
		try {
			props.store(new FileOutputStream(new File("javver.properties")), null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
